package com.example.rxjavademo1.views;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.example.rxjavademo1.R;
import com.example.rxjavademo1.adapter.TrackingAdapter;
import com.example.rxjavademo1.base.BaseActivity;
import com.example.rxjavademo1.contract.TrackingContract2;
import com.example.rxjavademo1.entity.TrackingNum;
import com.example.rxjavademo1.presenter.TrackingPresenter2;
import com.example.rxjavademo1.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class TrackingActivity2 extends BaseActivity
        implements TrackingContract2.ITrackingView, View.OnClickListener {
    private TrackingContract2.ITrackingPresenter mPresenter;
    private Button mButton;
    private EditText mEdit;
    private TextView mText;
    private RecyclerView mRecyclerView;
    private List<TrackingNum.DataDTO.TrackingDetailsDTO> mList;
    private TrackingAdapter mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_tracking;
    }

    @Override
    public void init() {
        mPresenter = new TrackingPresenter2(this, this);
        mButton = findViewById(R.id.button);
        mEdit = findViewById(R.id.edit);
        mText = findViewById(R.id.text);
        mRecyclerView = findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mList = new ArrayList<>();
        mAdapter = new TrackingAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        mButton.setOnClickListener(this);
    }

    @Override
    public void showData(TrackingNum.DataDTO data) {
        if (data != null) {
            LogUtils.d("asdf", "track : " + data);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("快递名称 : ").append(data.getExpressName()).append("\n");
            stringBuilder.append("快递单号 : ").append(data.getTrackingNumber()).append("\n");
            stringBuilder.append("运输时长 : ").append(data.getTransitTime()).append("\n");
            stringBuilder.append("上次更新 : ").append(data.getLatestUpdateTime()).append("\n");
            stringBuilder.append("当前状态 : ").append(data.getLatestShipmentStatus()).append("\n");
            mText.setText(stringBuilder.toString());
            mList = data.getTrackingDetails();
            mAdapter.updateData(mList);
        } else {
            showToast("未查到数据，请输入正确单号！");
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {
            String trackingNumber = mEdit.getText().toString();
            if (!TextUtils.isEmpty(trackingNumber)) {
                mPresenter.getData(trackingNumber);
            }
        }
    }

    @Override
    public void onHttpLoading(boolean loading) {
        if (loading) {
            showLoading();
        } else {
            dismissLoading();
        }
    }

    @Override
    public void onHttpFail(String... msg) {
        if (msg != null && msg.length > 0) {
            showToast(msg[0]);
        }
    }

    @Override
    public void onHttpTimeout() {

    }

    @Override
    public void onHttpTokenExpiration() {

    }
}